package la.bbs558.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import la.bbs558.context.PointContext;
import la.bbs558.entity.User;
import la.bbs558.entity.UserData;
import la.bbs558.entity.vo.BbsPostVO;
import la.bbs558.entity.vo.BbsReplyVO;
import la.bbs558.entity.vo.UserCenterVO;
import la.bbs558.entity.enums.PointsOptType;
import la.bbs558.entity.enums.UserOpt;
import la.bbs558.entity.params.UserCenterSearchPage;
import la.bbs558.entity.params.UserUpdateParams;
import la.bbs558.service.*;
import la.bbs558.util.Objs;
import la.bbs558.util.Strings;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author: Shanks
 * @date: 2023/12/17 16:03
 * @desc:
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserCenterServiceImpl implements UserCenterService {

    private final UserDataService userDataService;

    private final UserService userService;

    private final BbsPostService bbsPostService;

    private final BbsReplyService bbsReplyService;
    private final UserPointsLogService userPointsLogService;

    private Long getUserId(Long userId) {
        userId = Objects.isNull(userId) ? StpUtil.getLoginIdAsLong() : userId;
        return userId;
    }

    @Override
    public UserCenterVO getUserStatistics(Long userId) {
        userId = getUserId(userId);
        UserData userData = userDataService.initGetUserData(userId);
        User user = userService.getById(userId);
        return new UserCenterVO()
                .setUserId(userId)
                .setNickname(user.getNickname())
                .setAvatar(user.getAvatar())
                .setSign(user.getSign())
                .setPostCnt(userData.getPostCnt())
                .setReplyCnt(userData.getReplyCnt())
                .setFollowCnt(userData.getFollowCnt())
                .setFansCnt(userData.getFansCnt());
    }

    @Override
    public void getUserPost(UserCenterSearchPage<BbsPostVO> searchPage) {
        searchPage.setUserId(getUserId(searchPage.getUserId()));
        bbsPostService.listByUserId(searchPage);
    }

    @Override
    public void getUserReply(UserCenterSearchPage<BbsReplyVO> searchPage) {
        searchPage.setUserId(getUserId(searchPage.getUserId()));
        bbsReplyService.listByUserId(searchPage);
    }

    @Override
    public UserCenterVO getUserInfo(Long userId) {
        User user = userService.getById(userId);
        UserData userData = userDataService.initGetUserData(userId);
        return new UserCenterVO()
                .setUserId(userId)
                .setAvatar(user.getAvatar())
                .setNickname(user.getNickname())
                .setSex(user.getSex())
                .setEmail(user.getEmail())
                .setTel(user.getTel())
                .setCreateTime(user.getCreateTime())
                .setSign(user.getSign())
                .setFansCnt(userData.getFansCnt())
                .setFollowCnt(userData.getFollowCnt())
                .setPostCnt(userData.getPostCnt())
                .setReplyCnt(userData.getReplyCnt())
                ;
    }

    @Override
    public void updateInfo(UserUpdateParams params) {
        String nickname = params.getNickname();
        Objs.isTrue(Strings.isNotEmpty(nickname), "昵称不能为空");
        // TODO 检测昵称和签名是否非法文字，暂时不检测
//        Objs.isTrue(!SensitiveWordHelper.contains(nickname), "昵称不能包含非法文字");
        String sign = params.getSign();
//        Objs.isTrue(!SensitiveWordHelper.contains(sign), "签名不能包含非法文字");
        String tel = params.getTel();
        Objs.isTrue(Strings.isEmpty(tel) || tel.length() <= 30, "联系方式不能超过30个字符");
        Objs.isTrue(nickname.length() <= 20, "昵称不能超过20个字符");
        Objs.isTrue(sign.length() <= 20, "个人签名不能超过20个字符");
        Long userId = StpUtil.getLoginIdAsLong();
        boolean update = new User()
                .setId(userId)
                .setNickname(nickname)
                .setSign(sign)
                .setSex(params.getSex())
                .setTel(tel)
                .updateById();
        PointContext context = new PointContext(userId,userId,PointsOptType.SET_AVATAR, UserOpt.SET_AVATAR);
        if(Objs.isValid(tel)){
            context.setOptType(PointsOptType.SET_TEL);
            context.setOptDetail(UserOpt.SET_TEL);
            userPointsLogService.getPoints(context);
        }
    }

    @Override
    public void updateAvatar(String key) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean res = userService.updateById(new User().setId(userId).setAvatar(key));
        if (res) {
            PointContext context = new PointContext(userId,userId,PointsOptType.SET_AVATAR, UserOpt.SET_AVATAR);
            userPointsLogService.getPoints(context);
        }
    }
}
